package com.emath.objects;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Bitmap;
import com.emath.shared.BitmapUtils;
import com.emath.shared.Globals;
import com.emath.shared.NetUtils;

public class Card {
	public int cardID;
	private String uniqueID;
	private String cardData;
	private String cardName;
	private String type;
	private String specialEffect;
	private String levelUp;
	private String wiki;
	private String music;
	private String creatorID;
	private Boolean playDeck;
	private String bornOn;

	private boolean tapped;
	double legacy, legend, challengeRating;
	public String cost;
	public String role;
	public String mind1, charm1, strength1;
	public String mind2, charm2, strength2;
	public String mind3, charm3, strength3;
	public String levelup, special, specialeffect, image, setName, movie,
			type1, type2, type3;
	public Bitmap bitmap;
	private String objectState = "";

	public String getCardID() {
		return "" + cardID;
	}

	public String getCardName() {
		return cardName;
	}

	public String getCharm1() {
		return charm1;
	}

	public String getCharm2() {
		return charm2;
	}

	public String getCharm3() {
		return charm3;
	}

	public String getCost() {
		return cost;
	}

	public String getCreatorID() {
		return creatorID;
	}

	public String getImage() {
		return image;
	}

	public String getLevelUp() {
		return levelUp;
	}

	public String getMind1() {
		return mind1;
	}

	public String getMind2() {
		return mind2;
	}

	public String getMind3() {
		return mind3;
	}

	public String getMovie() {
		return movie;
	}

	public String getMusic() {
		return music;
	}

	public String getSetName() {
		return setName;
	}

	public String getSpecial() {
		return special;
	}

	public String getSpecialEffect() {
		return specialEffect;
	}

	public String getStrength1() {
		return strength1;
	}

	public String getStrength2() {
		return strength2;
	}

	public String getStrength3() {
		return strength3;
	}

	public String getType() {
		return type;
	}

	public String getWiki() {
		return wiki;
	}

	public String getCardData() {
		return cardData;
	}

	public void setCardData(String cardData) {
		this.cardData = cardData;
	}

	public void setCardID(String cardID) {
		this.cardID = Integer.parseInt(cardID);
	}

	public void setCardID(int cardID) {
		this.cardID = cardID;
	}

	public String getBornOn() {
		return bornOn;
	}

	public void setBornOn(String timeStamp) {
		this.bornOn = timeStamp;
	}

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}

	public void setCharm1(String charm1) {
		this.charm1 = charm1;
	}

	public void setCharm2(String charm2) {
		this.charm2 = charm2;
	}

	public void setCharm3(String charm3) {
		this.charm3 = charm3;
	}

	public void setCost(String cost) {
		this.cost = cost;
	}

	public void setCreatorID(String creatorID) {
		this.creatorID = creatorID;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public void setLevelUp(String levelUp) {
		this.levelUp = levelUp;
	}

	public void setMind1(String mind1) {
		this.mind1 = mind1;
	}

	public void setMind2(String mind2) {
		this.mind2 = mind2;
	}

	public void setMind3(String mind3) {
		this.mind3 = mind3;
	}

	public void setMovie(String movie) {
		this.movie = movie;
	}

	public void setMusic(String music) {
		this.music = music;
	}

	public void setSetName(String setName) {
		this.setName = setName;
	}

	public void setSpecial(String special) {
		this.special = special;
	}

	public void setSpecialEffect(String specialEffect) {
		this.specialEffect = specialEffect;
	}

	public Boolean getPlayDeck() {
		return this.playDeck;
	}

	public void setPlayDeck(Boolean playDeck) {
		this.playDeck = playDeck;
	}

	public void setStrength1(String strength1) {
		this.strength1 = strength1;
	}

	public void setStrength2(String strength2) {
		this.strength2 = strength2;
	}

	public void setStrength3(String strength3) {
		this.strength3 = strength3;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setWiki(String wiki) {
		this.wiki = wiki;
	}

	public String getObjectState() {
		return objectState;
	}

	public void setObjectState(String objectState) {
		this.objectState = objectState;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public String getUniqueID() {
		return uniqueID;
	}

	public void setUniqueID(String uniqueID) {
		this.uniqueID = uniqueID;
	}

	public Card(int id) {
		this.cardID = id;
		mind1 = "0";
		charm1 = "0";
		strength1 = "0";

		tapped = false; // The card can be tapped once it is put into play
						// anyway
	}

	public Card() {
		// TODO Auto-generated constructor stub
	}

	// May want to use a tap / untap function instead
	public void tap() {
		tapped = !tapped;
	}

	public void setattributeslevel1(String mind, String charm, String strength) {
		this.mind1 = mind;
		this.charm1 = charm;
		this.strength1 = strength;

	}

	public void settype1(String type1) {
		this.type1 = type1;
	}

	public void setBitmapAfterImageSet() throws Exception {
		Globals.numOfBitmapsProcessing++;
		final String URL = this.image;
		this.bitmap = BitmapUtils.loadBitmap(URL);
		Globals.numOfBitmapsProcessing--;
		/*
		 * new Thread(new Runnable() { public void run() {
		 * setBitmap(BitmapUtils.loadBitmap(URL)); } }).start();
		 */
	}

	public void alterPlayDeck() throws Exception {
		setObjectState("working");
		final String URL = "http://emath-project.appspot.com/emath/"
				+ "emathservicehttp?function=alterplaydeck&cardID="
				+ this.cardID + "&playDeck=" + this.playDeck + "&bornOn="
				+ URLEncoder.encode(this.bornOn, "UTF-8");
		new Thread(new Runnable() {
			public void run() {
				try {
					HttpClient httpclient = new DefaultHttpClient();
					// Prepare a request object
					HttpGet httpget = new HttpGet(URL);
					httpclient.execute(httpget);
					httpclient.getConnectionManager().shutdown();
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					setObjectState("free");
				}
			}
		}).start();
	}

	public void sqlGetCard() throws Exception {
		final String URL = "http://emath-project.appspot.com/emath/"
				+ "emathservicehttp?function=card&cardID=" + this.cardID;// gets
																			// a
																			// data
																			// on
																			// a
																			// particular
																			// cardID
																			// from
																			// the
																			// library.
																			// Not
																			// a
																			// unique
																			// card.
		new Thread(new Runnable() {
			public void run() {
				try {
					setObjectState("working");
					HttpClient httpclient = new DefaultHttpClient();
					// Prepare a request object
					HttpGet httpget = new HttpGet(URL);
					// Execute the request
					HttpResponse response;
					response = httpclient.execute(httpget);
					// Get hold of the response entity
					HttpEntity entity = response.getEntity();
					// If the response does not enclose an entity, there is no
					// need
					// to worry about connection release
					if (entity != null) {
						// A Simple JSON Response Read
						InputStream instream = entity.getContent();
						String result = NetUtils
								.convertStreamToString(instream);
						// A Simple JSONObject Creation
						JSONObject json = new JSONObject(result);
						setCardName(json.getString("cardName"));
						setType(json.getString("type"));
						setLevelUp(json.getString("levelUp"));
						setMind1(json.getString("mind1"));
						setMind2(json.getString("mind2"));
						setMind3(json.getString("mind3"));
						setCharm1(json.getString("charm1"));
						setCharm2(json.getString("charm2"));
						setCharm3(json.getString("charm3"));
						setStrength1(json.getString("strength1"));
						setStrength2(json.getString("strength2"));
						setStrength3(json.getString("strength3"));
						setSpecial(json.getString("special"));
						setSpecialEffect(json.getString("specialEffect"));
						setCost(json.getString("cost"));
						setWiki(json.getString("wiki"));
						setMovie(json.getString("movie"));
						setMusic(json.getString("music"));
						setImage(json.getString("image"));
						setBitmapAfterImageSet();
						// Closing the input stream will trigger connection
						// release
						setObjectState("free");
						instream.close();
						httpclient.getConnectionManager().shutdown();
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

}
